import http,pdb
from http import server
from . import sites


class PhotoServer(server.BaseHTTPRequestHandler):
    def do_GET(self):
        url='.'+self.requestline.split(' ')[1]
        '''
        requestline is something like this:
        GET /url/path/ HTTP/1.1
        '''
        if url[-1]=='/':url=url[:-1]
        self.url=url
        dispatch={
                './upload':sites.upload,
                './wantMyself':sites.wantMyself,
                '.':sites.viewAll,
                }
        toRun=dispatch.get(self.url,sites.filesystem)
        toRun(self)
        
    def do_POST(self):
        url='.'+self.requestline.split(' ')[1]
        if url[-1]=='/':url=url[:-1]
        self.url=url
        dispatch={
                './upload':sites.handle_uploaded_data,
                './wantMyself':sites.handle_wanted_data,
                }
        toRun=dispatch.get(self.url,sites.filesystem)
        toRun(self)

def run(server_class=server.HTTPServer,handler_class=PhotoServer):
    addr=('',8080)
    httpServer=server_class(addr,handler_class)
    httpServer.serve_forever()

if __name__=='__main__':
    run()
